<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <div class="header">
        <h3>小黑记事本</h3>
        <input type="text" placeholder="请输入内容" v-model="todoName" @keydown.enter="add" />
        <button class="add" @click="add" @keydown.enter="add">添加任务</button>
      </div>

      <ul class="todo-list">
        <li class="todo" v-for="(item,index) in list" :key="item.id">
          <div class="view">
            <span class="index">{{index+1}}.</span> <label>{{item.name}}</label>
            <button class="destory" @click="del(item.id)">删除</button>
          </div>
        </li>
      </ul>
      <div class="footer" v-show="list.length >0">
        <span class="todo-count">
          合计：<strong>{{list.length}}</strong> 个</span
        >
        <button class="clear-completed" @click="clear">清除任务</button>
      </div>
    </div>
    <script src="../js/vue.js"></script>
    <script>
      var app = new Vue({
        el: "#app",
        data: {
          todoName: "",
          list: [
            { id: 1, name: "跳绳20分钟" },
            { id: 2, name: "游泳40分钟" },
            { id: 3, name: "跑步30分钟" },
          ],
        },
        methods: {
          del(id) {
            this.list = this.list.filter((item) => item.id !== id);
          },
          clear() {
            this.list = [];
          },
          add() {
            if (this.todoName.trim() === "" || this.todoName.trim() === null) {
              alert("输入内容不可以为空");
            } else {
              this.list.unshift({
                id: new Date().getTime(),
                name: this.todoName,
              });
            }
            this.todoName = "";
          },
        },
      });
    </script>
  </body>
</html>
